/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdmitPatient.java
 *
 * Created on Oct 22, 2011, 5:45:44 PM
 */
package pkg1;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Instances of this object represent forms allowing the user
 * to admit patients into vacant and clean beds.
 * @author Nick
 */
public class AdmitPatient extends javax.swing.JFrame {

    /** Creates new form AdmitPatient */
    public AdmitPatient(Bed bed) {
        this.bed = bed;
        this.setTitle("Patient Admitance");
        this.setLocationRelativeTo(null);
        initComponents();
    }

    
    
    // Initializes the GUI for the Admit Patient Form
    private void initComponents() {
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label4 = new java.awt.Label();
        label5 = new java.awt.Label();
        jButton1 = new javax.swing.JButton();
        jButton1.addMouseListener(new MouseHandler());
        age = new javax.swing.JTextField();
        gender = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        doctorModel = new DefaultListModel();
        jList1 = new javax.swing.JList(doctorModel);
        jScrollPane2 = new javax.swing.JScrollPane();
        nurseModel = new DefaultListModel();
        jList2 = new javax.swing.JList(nurseModel);
        name = new javax.swing.JTextField();
        label6 = new java.awt.Label();
        context = Context.getContext();
        floorContext = context.getFloor(bed);
        
        setUpDoctorModel();
        setUpNurseModel();
        
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        label1.setText("Date Of Birth: ");

        label2.setText("Gender: ");

        label4.setText("Doctor: ");

        label5.setText("Nurse: ");

        jButton1.setText("Admit");

        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setViewportView(jList2);

        label6.setText("Name: ");
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                    .addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gender, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(185, 185, 185)
                        .addComponent(jButton1)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(gender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        )));

        pack();
    }// </editor-fold>
    
    //Sets up the Doctor's list 
    private void setUpDoctorModel() {
        for(Doctor doctor : context.getDoctors()) {
            doctorModel.addElement(doctor);
        }
    }
    
    // Sets up the Nurse's list
    private void setUpNurseModel() {
        for(Nurse nurse: context.getNurses()) {
            nurseModel.addElement(nurse);
        }
    }
    
    //Sets up the mouse Handler for the admit button
    private class MouseHandler extends MouseAdapter {
      public void mouseClicked(MouseEvent event) {
         // add a new square if the cursor isn't inside a square
          try {
              if(!age.getText().equals("")) {
                  int monthAsInteger = Integer.parseInt(age.getText().substring(0, 2)) - 1;
                  int dayAsInteger = Integer.parseInt(age.getText().substring(2, 4)); 
                  int yearAsInteger = Integer.parseInt(age.getText().substring(4, 8)) - 1900; 
                  if(((monthAsInteger > 12) || monthAsInteger < 0) || ((dayAsInteger > 31) || (dayAsInteger < 0)) ||
                          ((yearAsInteger > 2011) || (yearAsInteger < 0))){
                    JOptionPane.showMessageDialog(AdmitPatient.this, "Please enter a valid Date of Birth: 'mmddyyyy'");
                    return;
                  }
              }
          }
          catch(Exception e) {
              JOptionPane.showMessageDialog(AdmitPatient.this, "Please enter a valid Date of Birth: 'mmddyyyy'");
              return;
         }
         if(!gender.getText().equals("male") && !gender.getText().equals("female")){
             JOptionPane.showMessageDialog(AdmitPatient.this, "Please enter either 'male' or 'female' for gender.");
             return;
         }
         if(jList1.getSelectedValue() == null) {
             JOptionPane.showMessageDialog(AdmitPatient.this, "Please select a Doctor from the list.");
             return;
         }
         if(jList2.getSelectedValue() == null) {
             JOptionPane.showMessageDialog(AdmitPatient.this, "Please select a nurse from the list.");
             return;
         }
         else{
             Date date = new Date();
             String legitAge = age.getText();
             if(legitAge.equals("")) {
                 legitAge = "01010000";
             }
             Date dateOfBirth = convertStringToDate(legitAge);
             String legitName = name.getText();
             if(legitName.equals("")) {
                 floorContext.incrementNumberOfAnnonymousPatients();
                 legitName = "John Doe" + floorContext.getNumberOfAnnonymousPatients();
             }
             Patient p = new Patient(legitName, bed.isCriticalCare(), new Date(), 
                     gender.getText(), dateOfBirth, floorContext.createNewPatientId(), bed.getId());
             Doctor doctor = (Doctor)jList1.getSelectedValue();
             Nurse nurse = (Nurse)jList2.getSelectedValue();
             floorContext.admit(p, doctor, nurse, bed);
             JOptionPane.showMessageDialog(AdmitPatient.this, "Patient Admitted!");
             AdmitPatient.this.dispose();
         }
      }
    }
    
    /**
     * helper method which converts an entered date into its given date value.
     * @param date
     * @return 
     */
    private Date convertStringToDate(String date) {
        int month = Integer.parseInt(date.substring(0, 2)) - 1;
        int day = Integer.parseInt(date.substring(2,4));
        int year = Integer.parseInt(date.substring(4,8)) - 1900;
        return new Date(year, month, day);
    }
    // Variables declaration
    private Bed bed;
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField age;
    private javax.swing.JTextField gender;
    private javax.swing.JTextField name;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.Label label6;
    private DefaultListModel doctorModel;
    private DefaultListModel nurseModel;
    private FloorContext floorContext;
    private Context context;
    // End of variables declaration
}
